package com.footair.dy;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.webkit.JavascriptInterface;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

public class MainActivity extends AppCompatActivity {

    private WindowManager mWindowManager;
    private Button btn1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = findViewById(R.id.popbtn);

//        PlayManager.getIntance().start(this);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                initWindow();
//                PlayManager.getIntance().start(MainActivity.this);
            }
        });
        WebView webView = findViewById(R.id.wv_content);
        TextView textView = findViewById(R.id.tv_core);
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setAllowFileAccess(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        settings.setSupportZoom(true);
        settings.setBuiltInZoomControls(true);
        settings.setUseWideViewPort(true);
        settings.setSupportMultipleWindows(true);
        // webSetting.setLoadWithOverviewMode(true);
        settings.setAppCacheEnabled(true);
        // webSetting.setDatabaseEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setGeolocationEnabled(true);
        settings.setAppCacheMaxSize(Long.MAX_VALUE);
        // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
        settings.setPluginState(WebSettings.PluginState.ON_DEMAND);
        settings.setPluginsEnabled(true);
        // webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        settings.setUserAgentString("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36");

        WebViewClient webViewClient = new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView webView, String s) {
                webView.loadUrl(s);
                return true;
            }

            @Override
            public void onPageFinished(WebView webView, String s) {
                super.onPageFinished(webView, s);
//                String strTitle = webView.getTitle();
                String jsClick = "";
//                if (strTitle == "斗鱼 - 每个人的直播平台") {
//
//                } else {
//
//                }
                jsClick = "var script = document.createElement('script');";
                jsClick += "script.type = 'text/javascript';";
//                jsClick += "var loginA = document.getElementsByTagName('div');";
//                jsClick += "for(var i in loginA) {";
//                jsClick += "if(loginA[i].className == 'Header-login-wrap') {";
//                jsClick += "loginA[i].off();";
//                jsClick += "loginA[i].click();";
//                jsClick += "}}";

//                jsClick += "var inputNum = document.getElementById('login-tel');";
//                jsClick += "var inputPwd = document.getElementById('login-pwd');";
////                jsClick += "inputNum.click();";
//                jsClick += "inputNum.value = '150';";
//                jsClick += "inputPwd.value = '333';";
//                jsClick += "var title = document.getElementsByTagName('title')[0];";
//                jsClick += "if (title.text == '斗鱼 - 每个人的直播平台') {";
//                //                jsClick += "var rootDiv = document.getElementsById('root');";
//                jsClick += "var allDivs = document.getElementsByTagName('div');";
//                jsClick += "for (var i=0;i<allDivs.length;i++) {";
//                jsClick += "if(allDivs[i].className == 'HomeHeader-chargeB'){";
//                jsClick += "var login = allDivs[i];";
//                jsClick += "login.click();";
//                jsClick += "}}}";
//                jsClick += "else if(title.text == '斗鱼登录'){";
//                jsClick += "var inputNum = document.getElementById('login-tel');";
//                jsClick += "var inputPwd = document.getElementById('login-pwd');";
////                jsClick += "inputNum.click();";
//                jsClick += "inputNum.value = '150';";
////                jsClick += "inputNum.val('15021731051');";
////                jsClick += "inputPwd.val('i15827033856');";
//                jsClick += "} else {}";
//                jsClick += "var inputNum = document.getElementById('login-tel');";
//                jsClick += "var inputPwd = document.getElementById('login-pwd');";
//                jsClick += "inputNum.click();";
//                jsClick += "inputNum.value = '150';";
//                jsClick += "inputPwd.value = '333';";
//                jsClick += "alert('123');";

//                jsClick = "(function() {";
//                jsClick += "function clickTest(){";
//                jsClick += "var inputNum = document.getElementById('login-tel');";
//                jsClick += "var inputPwd = document.getElementById('login-pwd');";
//                jsClick += "var loginBtn = document.getElementById('btn-login');";
//                jsClick += "inputNum.value = '15021731051';";
//                jsClick += "inputPwd.value = 'i15827033856';";
//                jsClick += "var divs = document.getElementsByTagName('div');";
//                jsClick += "for(var i=0;i<divs.length;i++) {";
//                jsClick += "if(divs[i].className == 'geetest_holder geetest_douyufullpage geetest_ready') {";
//                jsClick += "divs[i].click();";
//                jsClick += "}}";
//                jsClick += "}";
//                jsClick += "setTimeout('clickTest()','5000');";
//                jsClick += "function loginClick(){";
//                jsClick += "loginBtn.click();";
//                jsClick += "}";
//                jsClick += "setTimeout('loginCLick()','5000');";
//                jsClick += "})()";

                jsClick += "var spans = document.getElementsByTagName('span');";
                jsClick += "for(var i in spans) {";
                jsClick += "if (spans[i].className == 'scancide-to js-to-link js-need-param fr') {";
                jsClick += "spans[i].click();";
                jsClick += "}}";
                jsClick += "var loginBtn;";
                jsClick += "var inputs = document.getElementsByTagName('input');";
                jsClick += "for(var i in inputs) {";
                jsClick += "if(inputs[i].className == 'fr ipt ipt-need-parent country-phonenum') {";
                jsClick += "inputs[i].value = '15021731051';";
                jsClick += "}";
                jsClick += "if (inputs[i].className == 'ipt') {";
                jsClick += "inputs[i].value = 'i15827033856';";
                jsClick += "}";
                jsClick += "if (inputs[i].className == 'loginbox-sbt btn-sub') {";
                jsClick += "loginBtn = inputs[i];";
                jsClick += "}}";
                jsClick += "function loginClick(){";
                jsClick += "loginBtn.click();";
                jsClick += "}";
//                jsClick += "setTimeout('loginClick()','2000');";
                jsClick += "function verifyClick() {";
                jsClick += "var inputsBtns = document.getElementsByTagName('input');";
                jsClick += "for(var i in inputsBtns) {";
                jsClick += "if(inputsBtns[i].className == 'location-verify-vbtn js-verify-voicebtn') {";
                jsClick += "inputsBtns[i].click();";
                jsClick += "}}}";
                jsClick += "setTimeout('verifyClick()','3000');";

//                String js = "var script = document.createElement('script');";
//                js += "script.type = 'text/javascript';";
//                js += "var video_douyu=document.getElementsByTagName('video')[0];";
////                js += "video_douyu.play();";
//                js += "video_douyu.muted=true;";
                webView.loadUrl("javascript:" + jsClick);
            }
        };
        webView.setWebViewClient(webViewClient);
//        webView.loadUrl("https://www.douyu.com/topic/S9_score?rid=96291");
        webView.loadUrl("http://passport.douyu.com");
        webView.addJavascriptInterface(new jsInterface(),"interface");
        if (webView.getX5WebViewExtension() != null) {
            textView.setText("X5");
        } else {
            textView.setText("SYS");
        }
//        initWindow();
    }

    private final class jsInterface{
        @SuppressLint("JavascriptInterface")
        @JavascriptInterface
        public void register(String userInfo){
            Toast.makeText(MainActivity.this, userInfo, Toast.LENGTH_LONG).show();
        }
    }

    public void click(View view) {
        switch (view.getId()) {
            case R.id.popbtn:
//                PlayManager.getIntance().start(this);
                break;
                default:
                    break;
        }
    }

    private void initWindow() {
        mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        int screenWidth = mWindowManager.getDefaultDisplay().getWidth();
        int screenHeight = mWindowManager.getDefaultDisplay().getHeight();
//        WebView view = new WebView(getApplicationContext());
        WebView view = new WebView(getApplicationContext());
//        TextView textView = findViewById(R.id.tv_core);
        WebSettings settings = view.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setAllowFileAccess(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        settings.setSupportZoom(true);
        settings.setBuiltInZoomControls(true);
        settings.setUseWideViewPort(true);
        settings.setSupportMultipleWindows(true);
        // webSetting.setLoadWithOverviewMode(true);
        settings.setAppCacheEnabled(true);
        // webSetting.setDatabaseEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setGeolocationEnabled(true);
        settings.setAppCacheMaxSize(Long.MAX_VALUE);
        // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
        settings.setPluginState(WebSettings.PluginState.ON_DEMAND);
        settings.setPluginsEnabled(true);
        // webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        WebViewClient webViewClient = new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView webView, String s) {
                webView.loadUrl(s);
                return true;
            }

            @Override
            public void onPageFinished(WebView webView, String s) {
                super.onPageFinished(webView, s);
                String js = "var script = document.createElement('script');";
                js += "script.type = 'text/javascript';";
                js += "var video_douyu=document.getElementsByTagName('video')[0];";
                js += "video_douyu.play();";
                //                js += "video_douyu.muted=true;";
                webView.loadUrl("javascript:" + js);
            }
        };
        view.setWebViewClient(webViewClient);
        view.loadUrl("https://m.douyu.com/78561");


        //        X5WebView view = new X5WebView(getApplicationContext(), null);
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.gravity = Gravity.LEFT | Gravity.TOP;
        lp.x = screenWidth;
        lp.y = screenHeight;

        lp.type = WindowManager.LayoutParams.TYPE_PHONE;
        lp.format = PixelFormat.RGBA_8888;
        lp.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        lp.gravity = Gravity.LEFT | Gravity.TOP;
        //        lp.width = screenWidth;
        //        lp.height = screenHeight;
        lp.width = 200;
        lp.height = 200;
        //        lp.width = 0;
        //        lp.height = 0;
        view.setLayoutParams(lp);
        mWindowManager.addView(view, lp);
//        mAllView.add(view);
    }
}
